﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using DevExpress.Web.ASPxEditors;

public partial class ConsumerManage_ConsumerQuick : Frame.Pages.BasePage, Frame.Pages.IDetailEditManPage<DAL.BI_Consumer>
{
    public string ForwardAction
    {
        get
        {
            return Request.QueryString["action"] ?? string.Empty;
        }
    }
    public string ConsumerID
    {
        get
        {
            return Request.QueryString["Consumerid"] ?? string.Empty;
        }
    }
    protected override void Page_Load(object sender, EventArgs e)
    {

        base.Page_Load(sender, e);
        if (!string.IsNullOrEmpty(ConsumerID) && !string.IsNullOrEmpty(ForwardAction))
        {
            using (DAL.CMSDALDataContext DataContext = new DAL.CMSDALDataContext())
            {
                DAL.BI_Consumer model = DataContext.BI_Consumer.FirstOrDefault(o => o.ConsumerID == int.Parse(ConsumerID));
                Model = model;
                BackUrl = "~/ConsumerManage/ConsumerQuick.aspx";
                Frame.Pages.RefrenceContext<DAL.BI_Consumer>.UpdateCurrent(this);
                switch (ForwardAction.ToLower())
                {
                    case "book":
                        Response.Redirect("~/ConsumerManage/EditConsumerBook.aspx");
                        break;
                    case "his":
                        Response.Redirect("~/ConsumerManage/EditConsumerHis.aspx");
                        break;
                    default:
                        Server.Transfer("~/ConsumerManage/ViewDetailConsumer.aspx");
                        break;
                }

            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.tbxSearch.Text.Trim()))
        {
            this.tbxSearch.Text = string.Empty;
            return;
        }

        using (DAL.CMSDALDataContext DataContext = new DAL.CMSDALDataContext())
        {
            int Count = DataContext.BI_Consumer.Count(o => o.PhoneNum.Contains(this.tbxSearch.Text.Trim()) ||
                (o.PhoneRemark != null && o.PhoneRemark.Contains(this.tbxSearch.Text.Trim())) ||
                o.CName.Contains(this.tbxSearch.Text.Trim()) || o.Company.Contains(this.tbxSearch.Text.Trim()));
            if (Count <= 0)
            {
                Frame.Global.GlobalFunctions.Alert("不存在此号码,姓名或单位对应的客户！");
            }
            else if (Count == 1)
            {
                DAL.BI_Consumer model = DataContext.BI_Consumer.First(o => o.PhoneNum.Contains(this.tbxSearch.Text.Trim()) ||
                (o.PhoneRemark != null && o.PhoneRemark.Contains(this.tbxSearch.Text.Trim())) ||
                o.CName.Contains(this.tbxSearch.Text.Trim()) || o.Company.Contains(this.tbxSearch.Text.Trim()));
                Model = model;
                BackUrl = "~/ConsumerManage/ConsumerQuick.aspx";
                Frame.Pages.RefrenceContext<DAL.BI_Consumer>.UpdateCurrent(this);
                switch (ForwardAction.ToLower())
                {
                    case "book":
                        Response.Redirect("~/ConsumerManage/EditConsumerBook.aspx");
                        break;
                    case "his":
                        Response.Redirect("~/ConsumerManage/EditConsumerHis.aspx");
                        break;
                    default:
                        Server.Transfer("~/ConsumerManage/ViewDetailConsumer.aspx");
                        break;
                }

            }
            else
            {
                this.MutileResult.Visible = true;
                this.QueryTable.Visible = false;
                gridView.DataSource = DataContext.BI_Consumer.Where(o => o.PhoneNum.Contains(this.tbxSearch.Text.Trim()) ||
                (o.PhoneRemark != null && o.PhoneRemark.Contains(this.tbxSearch.Text.Trim())) ||
                o.CName.Contains(this.tbxSearch.Text.Trim()) || o.Company.Contains(this.tbxSearch.Text.Trim())).ToList();
                gridView.DataBind();
            }


        }

    }
    protected string GetCompanyName(object CompanyID)
    {
        if (Cache["CompanyList"] == null)
        {
            using (DAL.CMSDALDataContext context = new DAL.CMSDALDataContext())
            {
                Cache["CompanyList"] = context.BI_Company.ToList();
            }
        }
        List<BI_Company> list = Cache["CompanyList"] as List<BI_Company>;
        BI_Company model = list.FirstOrDefault(o => o.CompanyID == (int)CompanyID);
        if (model != null)
        {
            return model.CompanyName;
        }
        return string.Empty;
    }


    #region IDetailEditManPage<BI_Consumer> 成员

    public string BackUrl
    {
        get;
        set;
    }

    public DAL.BI_Consumer Model
    {
        get;
        set;
    }

    public string ScriptBackUrl
    {
        get;
        set;
    }

    #endregion

    protected override string ActionID
    {
        get { return "0"; }
    }

    protected void gridView_HtmlRowPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
    {
        if (e.RowType == DevExpress.Web.ASPxGridView.GridViewRowType.Data)
        {
            if (e.GetValue("IsVip").ToString() == "1")
            {
                e.Row.BackColor = System.Drawing.Color.Pink;
            }
        }
    }

    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
        System.Web.UI.WebControls.LinkButton btn = sender as System.Web.UI.WebControls.LinkButton;
        using (DAL.CMSDALDataContext DataContext = new DAL.CMSDALDataContext())
        {
            DAL.BI_Consumer model = DataContext.BI_Consumer.First(o => o.ConsumerID.ToString() == btn.CommandArgument);
            Model = model;
        }
        BackUrl = "~/ConsumerManage/ConsumerQuick.aspx";
        Frame.Pages.RefrenceContext<DAL.BI_Consumer>.UpdateCurrent(this);
        switch (ForwardAction.ToLower())
        {
            case "book":
                Response.Redirect("~/ConsumerManage/EditConsumerBook.aspx");
                break;
            case "his":
                Response.Redirect("~/ConsumerManage/EditConsumerHis.aspx");
                break;
            default:
                Server.Transfer("~/ConsumerManage/ViewDetailConsumer.aspx");
                break;
        }
    }
}